<?php
/*
 *   класс для реализации CRUD (create read update insert)
 *   @author ZloY max.zloy@gmail.com 
 */
abstract class ZloY_Model_CRUD {
	/*
	 * @var $_table 
	 * Класс табилицы
	 */
	protected $_table;
	/*
	 * @var $_row
	 * Класс строки
	 */
	protected $_row;
	
	public function __construct($id = null)
	{
		$this->_table = $this->getTable();
		if($id === null){
			$this->_row = $this->_table->createRow();
		} else {
			$this->_row = $this->_table->find($id)->current();
		}
	}
	/*
	 *  getters  and setters 
	 */
	public function __get($field)
	{
		if(isset($this->_row->$field)){
			return $this->_row->field;
		}
	}
	
	public function __set($field , $value)
	{
		if(isset($this->_row->$field)){
			$this->_rew->$field = $value;
		}
	}
	
	/*
	 * abstract methods
	 * @return клас таблицы Zend_Db_Table_Abstract
	 */
	abstract public function getTable();
	
	public function fill(array $data)
	{
		foreach($data as $key=>$value){
			if(isset($this->_row->$key)){
				$this->_row->$key = $value;
			}
		}
	}
	
	public function getRow()
	{
		return $this->_row;
	}
	
	public function save()
	{
		$this->_row->save();
		return $this->_row;
	}
	
	public function delete()
	{
		$this->_row->delete();
	}
	
	public function populate(){
		return $this->_row->toArray();
	}
}